package com.example.rpc_easy.proxy;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.example.rpc_easy.Serializable.JdkSerializable;
import com.example.rpc_easy.Serializable.Serializable1;
import com.example.rpc_easy.model.RpcRequestExample;
import com.example.rpc_easy.model.RpcResponseExample;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class ServiceProxy implements InvocationHandler {
    /**
     * 实现动态代理
     *
     * @param proxy  the proxy instance that the method was invoked on
     * @param method the {@code Method} instance corresponding to
     *               the interface method invoked on the proxy instance.  The declaring
     *               class of the {@code Method} object will be the interface that
     *               the method was declared in, which may be a superinterface of the
     *               proxy interface that the proxy class inherits the method through.
     * @param args   an array of objects containing the values of the
     *               arguments passed in the method invocation on the proxy instance,
     *               or {@code null} if interface method takes no arguments.
     *               Arguments of primitive types are wrapped in instances of the
     *               appropriate primitive wrapper class, such as
     *               {@code java.lang.Integer} or {@code java.lang.Boolean}.
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Serializable1 serializable1 = new JdkSerializable();
        // 优先处理Object类的方法
        if (method.getDeclaringClass() == Object.class) {
            try {
                return method.invoke(this, args);  // 使用this而不是proxy，避免递归调用
            } catch (Exception e) {
                throw new RuntimeException("Object方法调用失败", e);
            }
        }

        //构建代理对象
        RpcRequestExample rpcRequestExample =
                new RpcRequestExample().builder()
//                       .serviceName(proxy.getClass().getName())
//                       .serviceName(method.getDeclaringClass().getName())
                        .serviceName(method.getDeclaringClass().getName())
                        .args(args)
                        .parameterTypes(method.getParameterTypes())
                        .methodName(method.getName())
                        .build();
        //序列化对象
//        System.out.println("Method declared in: " + method.getDeclaringClass().getName());
        byte[] serialize = serializable1.serialize(rpcRequestExample);

        try {
            HttpResponse response = HttpRequest.post("http://localhost:8080")
                    .body(serialize)
                    .execute();
            byte[] body = response.bodyBytes();
            RpcResponseExample deserialize = serializable1.deserialize(body, RpcResponseExample.class);
            return deserialize.getData();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}